package org.wanho.shop.service.impl;

import org.wanho.shop.dao.UserDao;
import org.wanho.shop.dao.impl.UserDaoImpl;
import org.wanho.shop.entity.User;
import org.wanho.shop.exception.LoginErrorException;
import org.wanho.shop.exception.UserExistsException;
import org.wanho.shop.service.UserService;
import org.wanho.shop.util.MD5Util;

/**
 * Author：汤小洋
 * Date：2023-04-26 9:38
 * Description：<描述>
 */
public class UserServiceImpl implements UserService {

    private UserDao userDao = new UserDaoImpl();

    @Override
    public User login(String username, String password) throws LoginErrorException {
        User user = userDao.selectByUsernameAndPassword(username, MD5Util.md5(password));
        if (user == null){
            throw new LoginErrorException("用户名或密码不正确！");
        }
        return user;
    }

    @Override
    public void register(User user) throws UserExistsException {
        // 判断用户名是否被占用
        User u = userDao.selectByUsername(user.getUsername());
        if(u != null){
            throw new UserExistsException("用户名已被注册！");
        }

        // 对密码进行加密
        user.setPassword(MD5Util.md5(user.getPassword()));

        userDao.insert(user);
    }
}
